package cn.it.trigger.http;

import cn.it.infrastructure.api.IGoodsApi;
import cn.it.infrastructure.api.dto.GoodsServiceRequestDTO;
import cn.it.infrastructure.api.dto.GoodsServiceResponseDTO;
import cn.it.infrastructure.api.dto.TypeServiceResponseDTO;
import cn.it.infrastructure.domain.service.IGoodsService;
import cn.it.infrastructure.po.TbGoods;
import cn.it.infrastructure.type.enums.ResponseCode;
import cn.it.infrastructure.type.exception.AppException;
import cn.it.infrastructure.type.model.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author Mzq
 * @description
 * @createTime 2024/7/4
 */
@Slf4j
@RestController
@CrossOrigin
@RequestMapping("/api/${app.config.api-version}/goods")
public class GoodsApiController implements IGoodsApi {

    private IGoodsService goodsService;

    public GoodsApiController(IGoodsService goodsService) {
        this.goodsService = goodsService;
    }

    /**
     * 查询商品
     *
     * @param request
     * @return curl --request POST \
     * --url http://localhost:8000/api/v1/goods/Inquire \
     * --header 'content-type: application/json' \
     * --data '{
     * "id" : 26,
     * "name" : null,
     * "typeId" : null,
     * "bsinessId" : null
     * }'
     */
    @RequestMapping(value = "Inquire", method = RequestMethod.POST)
    @Override
    public Response<GoodsServiceResponseDTO> queryGoodsById(@RequestBody GoodsServiceRequestDTO request) {
        try {
            Long id = Long.valueOf(request.getId());
            TbGoods tbGoods = goodsService.queryGoodsById(id);

            log.error("查询商品成功 goodsId:{} goodsName:{}", tbGoods.getId(), tbGoods.getName());

            return Response.<GoodsServiceResponseDTO>builder()
                    .code(ResponseCode.SUCCESS.getCode())
                    .info(ResponseCode.SUCCESS.getInfo())
                    .data(GoodsServiceResponseDTO.builder()
                            .id(tbGoods.getId())
                            .name(tbGoods.getName())
                            .realPrice(tbGoods.getRealPrice())
                            .discountPrice(tbGoods.getDiscountPrice())
                            .imageUrl(tbGoods.getImageUrl())
                            .norms(tbGoods.getNorms())
                            .stock(tbGoods.getStock())
                            .description(tbGoods.getDescription())
                            .isShow(tbGoods.getIsShow())
                            .build())
                    .build();
        } catch (AppException e) {
            log.error("查询商品失败 goodsId:{} ", request.getId(), e);
            return Response.<GoodsServiceResponseDTO>builder()
                    .code(e.getCode())
                    .info(e.getInfo())
                    .build();
        }
    }

    @RequestMapping(value = "query", method = RequestMethod.GET)
    @Override
    public Response<List<GoodsServiceResponseDTO>> queryRecommended() {
        try {
            List<TbGoods> goodsList = goodsService.queryRecommended();

            List<GoodsServiceResponseDTO> responseList = new ArrayList<>(goodsList.size());

            for (TbGoods tbGoods : goodsList) {
                GoodsServiceResponseDTO responseDTO = GoodsServiceResponseDTO.builder()
                        .id(tbGoods.getId())
                        .name(tbGoods.getName())
                        .realPrice(tbGoods.getRealPrice())
                        .discountPrice(tbGoods.getDiscountPrice())
                        .imageUrl(tbGoods.getImageUrl())
                        .norms(tbGoods.getNorms())
                        .stock(tbGoods.getStock())
                        .description(tbGoods.getDescription())
                        .isShow(tbGoods.getIsShow())
                        .build();
                responseList.add(responseDTO);
            }

            return Response.<List<GoodsServiceResponseDTO>>builder()
                    .code(ResponseCode.SUCCESS.getCode())
                    .info(ResponseCode.SUCCESS.getInfo())
                    .data(responseList)
                    .build();

        } catch (AppException e) {
            return Response.<List<GoodsServiceResponseDTO>>builder()
                    .code(ResponseCode.UN_ERROR.getCode())
                    .info(ResponseCode.UN_ERROR.getInfo())
                    .build();
        }


    }

    @RequestMapping(value = "content", method = RequestMethod.POST)
    @Override
    public Response<List<GoodsServiceResponseDTO>> queryGoodsByContent(@RequestParam String content) {
        try {
            List<TbGoods> goods = goodsService.queryGoodsByContent(content);

            List<GoodsServiceResponseDTO> responseList = new ArrayList<>(goods.size());

            for (TbGoods good : goods) {
                GoodsServiceResponseDTO response = GoodsServiceResponseDTO.builder()
                        .name(good.getName())
                        .realPrice(good.getRealPrice())
                        .discountPrice(good.getDiscountPrice())
                        .imageUrl(good.getImageUrl())
                        .norms(good.getNorms())
                        .stock(good.getStock())
                        .description(good.getDescription())
                        .isShow(good.getIsShow())
                        .build();
                responseList.add(response);
            }

            return Response.<List<GoodsServiceResponseDTO>>builder()
                    .code(ResponseCode.SUCCESS.getCode())
                    .info(ResponseCode.SUCCESS.getInfo())
                    .data(responseList)
                    .build();
        } catch (AppException e) {

        }
        return Response.<List<GoodsServiceResponseDTO>>builder()
                .code(ResponseCode.UN_ERROR.getCode())
                .info(ResponseCode.UN_ERROR.getInfo())
                .build();
    }


}
